package spider;

import constant.Common;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import util.AgentUtil;
import util.CollectionUtil;
import util.ImageUtils;
import util.StringUtil;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * @author zhanglinfeng
 * @date create in 2022/10/20 17:58
 */
public abstract class MyProcessor implements PageProcessor {
    /** 网站地址 */
    protected List<String> siteUrl;

    protected Logger LOGGER = LoggerFactory.getLogger(MyProcessor.class);
    protected Site site = Site.me().setRetryTimes(2).setSleepTime(5000).setTimeOut(10000).addHeader("user-agent", AgentUtil.get());

    @Override
    public abstract void process(Page page);

    @Override
    public Site getSite() {
        return site.setCharset(Common.UTF_8);
    }

    public BiConsumer<RemoteWebDriver, String> getMouseBehavior(){
//        return (driver,url) -> {
//            WebElement webElement = driver.findElement(By.className(""));
//            if (webElement != null) {
//                Actions action = new Actions(driver);
//                action.moveToElement(webElement);
//                action.click();
//            }
//        };
        return null;
    }

    protected String dealDetailStyle(String detailStyle, String url) {
        if (StringUtils.isNotEmpty(detailStyle)) {
            return ImageUtils.handleImages(detailStyle, url);
        }
        return detailStyle;
    }

    protected void addHref(Page page, String cssQuery) {
        String pageAsXml = page.getHtml().toString();
        Document document = Jsoup.parse(pageAsXml);
        Element body = document.body();
        Elements elements = body.select(cssQuery);
        if (CollectionUtil.isNotEmpty(elements)) {
            for (Element element : elements) {
                String detailUrl = element.select("a").attr("href");
                if (StringUtil.isNotEmpty(detailUrl)) {
                    page.addTargetRequest(detailUrl);
                }
            }
            // TODO 是否已爬取过
           // page.getTargetRequests().removeIf(request -> );
        } else {
            dealPage(body, page.getUrl().toString());
        }
    }

    protected abstract void dealPage(Element body, String url);

}
